﻿using LuUpgrade.Controller;
using LuUpgrade.Model;
using LuUpgrade.Utils;


namespace LuUpgrade;

internal static class Program
{
    private static void Main()
    {
        var dbContext = new DbContext();

        var dbOperate = new DbOperate();


        // 数据迁移
        dbOperate.Mige();


        // 查询多条数据
        // var employees = dbOperate.GetEmployeeList();
        // foreach (var emp in employees)
        // {
        //     Console.WriteLine(
        //         $"{emp.Id},{emp.Username},{emp.Age},{emp.Gender},{emp.Province},{emp.Email},{emp.Phone},{emp.Education},{emp.Ip},{emp.Created}");
        // }


        // 查询单条数据
        //var server = dbOperate.GetServer();
        //if (server != null)
        //{
        //    Console.WriteLine($"ID-{server.Id}, 服务器名-{server.Name}, 地址-{server.GameAddr}");
        //}
        //else
        //{
        //    Console.WriteLine("查询为空!!");
        //}


        // 新增一条数据
        // var emp = new EmployeeEntity()
        // {
        //     Username = "李白4",
        //     Age = 21,
        //     Gender = "男",
        //     Province = "河南省",
        //     Email = "libai@tang.com",
        //     Phone = "18236322526",
        //     Education = "博士",
        //     CompanyId = 2,
        //     Ip = "127.0.0.1",
        //     Created = DateTime.Now

        // };

        // var id = dbOperate.AddOneEmployee(emp);
        // Console.WriteLine("插入成功，新用户的 ID 是：" + id);


        // var emp1 = dbContext.Db.Queryable<EmployeeEntity>().InSingle(4);
        // Console.WriteLine($"{emp1.Username}, {emp1.Age}, {emp1.Gender}, {emp1.Email}");

        // var emp2 = dbContext.Db.Queryable<EmployeeEntity>().InSingle(4);
        // Console.WriteLine($"{emp2.Username}, {emp2.Age}, {emp2.Gender}, {emp2.Email}");


        var temps = dbContext.Db.Queryable<EmployeeEntity>().Take(10000).ToList();
        foreach (var emp in temps)
        {
            Console.WriteLine($"{emp.Id}, {emp.Username}, {emp.Age}, {emp.Gender}, {emp.Email}, {emp.Created}");
        }


        // var res = dbOperate.DeleteEmployee(1);
        // System.Console.WriteLine($"删除数据-{res}");


        // Task.Run(AsyncPost).Wait();


        // string[] prefixes = {"http://127.0.0.1:8080/"};
        // NetServer server = new(prefixes);

        // Console.CancelKeyPress += (sender, e) =>
        // {
        //     server.Stop();
        //     e.Cancel = true;
        // };

        // server.Start();
    }


    private static async Task AsyncPost()
    {
        var netClient = new NetClient();

        var dbContext = new DbContext();
        const string url = "http://127.0.0.1:8008/test/";


        var results = await dbContext.Db.Queryable<EmployeeEntity>().Where(x => x.Id < 10).ToListAsync();


        foreach (var res in results)
        {
            try
            {
                var response = await netClient.SendJsonPostRequest(url, res);
                Console.WriteLine("POST 请求成功，响应内容：" + response);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


        // var requestData = new
        // {
        //     name = "李白",
        //     age = 21,
        //     email = "kadycui@tang.com"
        // };
    }
}